

package com.hazelcast.replicatedmap.impl.record;

import com.hazelcast.internal.serialization.Data;
import com.hazelcast.replicatedmap.impl.ReplicatedMapService;
import com.hazelcast.spi.impl.NodeEngine;

import static com.hazelcast.internal.namespace.NamespaceUtil.callWithNamespace;

/**
 * This is a {@link Data} based {@link ReplicatedRecordStore}
 * implementation
 */
public class DataReplicatedRecordStore extends AbstractReplicatedRecordStore<Data, Data> {

    private final NodeEngine nodeEngine;

    public DataReplicatedRecordStore(String name, ReplicatedMapService replicatedMapService, int partitionId) {
        super(name, replicatedMapService, partitionId);
        this.nodeEngine = replicatedMapService.getNodeEngine();
    }

    @Override
    public Object unmarshall(Object object) {
        String userCodeNamespace = replicatedMapConfig.getUserCodeNamespace();
        return object == null ? null : callWithNamespace(nodeEngine, userCodeNamespace, () -> nodeEngine.toObject(object));
    }

    @Override
    public Object marshall(Object object) {
        String userCodeNamespace = replicatedMapConfig.getUserCodeNamespace();
        return callWithNamespace(nodeEngine, userCodeNamespace, () -> nodeEngine.toData(object));
    }
}
